package com.example.customviewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * Created by ThinkPad on 2017/3/25.
 */

public class CustomAdapter extends PagerAdapter {
    private Context mContext;
    private ArrayList<Integer> mList = new ArrayList<>();
    private CustomViewpager mCustomViewpager;
    public CustomAdapter(Context context, ArrayList<Integer> list, CustomViewpager customViewpager) {
        mContext = context;
        mList = list;
        mCustomViewpager = customViewpager;
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(mContext);
        imageView.setBackgroundResource(mList.get(position));
        container.addView(imageView);
        mCustomViewpager.addChildView(imageView, position);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        mCustomViewpager.removeChildView(position);
    }
}
